package com.blog.module.news.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.blog.common.config.FileConfig;
import com.blog.common.controller.BaseAdminController;
import com.blog.common.mybatis.Page;
import com.blog.common.utils.FileUtil;
import com.blog.module.news.pojo.NewsInfo;
import com.blog.module.news.service.NewsInfoService;

/**
 * @Title: NewsInfoAdminController.java
 * @Description: TODO
 * @author user
 * @date 2016年7月28日 下午2:42:05
 * @version V1.0
 */
@Controller
@RequestMapping("admin/newsinfo/**")
public class NewsInfoAdminController extends BaseAdminController {

	@Autowired
	private NewsInfoService newsInfoService;

	/**
	 * @Title: pageNewsList
	 * @Description: 查询新闻類別
	 * @param tags
	 * @return String
	 * @throws
	 */
	@RequestMapping("list")
	public String pageNewsList(Model model, NewsInfo newsInfo) {
		Page page = newsInfoService.pageNewsInfoList(newsInfo);
		model.addAttribute("page", page);
		return "admin/news/list";
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "admin/news/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(NewsInfo newsInfo, @RequestParam(value = "image", required = false) CommonsMultipartFile file) {
		String imagePath = "";
        if (!file.isEmpty()) {
            imagePath = FileUtil.uploadFile(file, FileConfig.NEWS);
            newsInfo.setUrl(imagePath);
        }
		newsInfoService.insert(newsInfo);
		return redirect("/admin/newsinfo/list");
	}

	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable Integer id, Model model) {
		NewsInfo newsInfo = newsInfoService.selectById(id);
		model.addAttribute("newsInfo", newsInfo);
		return "admin/news/add";
	}

	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public String edit(NewsInfo news, @RequestParam(value = "image", required = false) CommonsMultipartFile file) {
		String imagePath = "";
        if (!file.isEmpty()) {
            imagePath = FileUtil.uploadFile(file, FileConfig.NEWS);
            news.setUrl(imagePath);
        }
		newsInfoService.insert(news);
		return redirect("/admin/newsinfo/list");
	}

	@RequestMapping(value = "/{id}/del", method = RequestMethod.POST)
	public String add(@PathVariable Integer id) {
		newsInfoService.del(id);
		return redirect("/admin/newsinfo/list");
	}
}
